// pages/bindingGuild/guild/guild.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: null,

    guilds: null,
    text_search: null,

    guilds_flag: true,

    //管理员身份
    guild_id: null,
    registrants_id: null,

    kongtu:false,


  },
  click_join: function(e) {
    // console.log(e.currentTarget.dataset.guild_id)
    this.setData({
      guild_id: e.currentTarget.dataset.guild_id,
      registrants_id: e.currentTarget.dataset.registrants_id,
    })
    wx.request({
      method: "POST",
      url: app.globalData.url + 'api/mine/guild/admin/apply',
      data: {
        token: app.globalData.token,
        deacon_id: this.data.registrants_id,
        guild_id: this.data.guild_id,
      },
      success: function(e) {
        // console.log(e)
        if (e.data.code == "-1") {
          wx.showToast({
            icon: "none",
            title: e.data.message,
          })
        } else {
          wx.showModal({
            content: '小主~您已成功提交审核，请及时联系公会长进行审核~',
            showCancel: false,
            confirmColor: "#ddba69",
            confirmText: "查看进度",
            success: function(e) {
              wx.navigateTo({
                url: '../../mine/guildManage/guildManage',
              })
            }
          })
        }
      }
    })
  },
  back_bind: function(e) {
    if (this.data.role != "3") {
      let guild_id = e.currentTarget.dataset.guild_id;
      let guild_name = e.currentTarget.dataset.guild_name;

      this.setData({
        p_guild_id: guild_id,
        p_guild_name: guild_name,
      })

      wx.redirectTo({
        url: '../anchor/authenticate/authenticate?p_guild_id=' + this.data.p_guild_id + '&&p_guild_name=' + this.data.p_guild_name,
      })
    }


  },
  inp_search: function(e) {
    // console.log(e.detail.value);
    this.setData({
      text_search: e.detail.value,
    })
  },
  click_search: function() {
    this.setData({
      kongtu: false,
    })
    // console.log("点击")
    const that = this;
    if (this.data.text_search) {
      this.setData({
        guilds_flag: false,
      })
      wx.request({
        method: "POST",
        url: app.globalData.url + 'api/mine/guild/admin/select',
        data: {
          token: app.globalData.token,
          search: this.data.text_search,
        },
        success: function(e) {
          console.log(e)
          if (e.data.data.admin==''){
            that.setData({
              guilds: e.data.data.admin,
              kongtu:true,
            })
          }else{
            that.setData({
              guilds: e.data.data.admin,
              kongtu: false,
            })
          }
          
        }
      })

    } else {
      this.setData({
        guilds_flag: true,
      })
      wx.request({
        method: "POST",
        url: app.globalData.url + 'api/mine/guild/admin/select',
        data: {
          token: app.globalData.token,
          search: this.data.text_search,
        },
        success: function(e) {
          console.log(e)
          that.setData({
            guilds: e.data.data.admin,
          })
        }
      })
    }



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      role: app.register_info.register_role,
    })
    const that = this;
    if (this.data.role != "3") {
      wx.request({
        method: "POST",
        url: app.globalData.url + 'api/mine/guild/admin/select',
        data: {
          token: app.globalData.token,
          search: this.data.text_search,
        },
        success: function(e) {
          // console.log(e)
          that.setData({
            guilds: e.data.data.admin,
          })
        }
      })
    } else {
      wx.request({
        method: "POST",
        url: app.globalData.url + 'api/mine/guild/admin/select',
        data: {
          token: app.globalData.token,
          search: this.data.text_search,
        },
        success: function(e) {
          console.log(e)
          that.setData({
            guilds: e.data.data.admin,
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})